package com.kuang.web.dao;

import com.kuang.web.pojo.Department;
import com.kuang.web.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@Repository
public class EmployeeDao {
    //用map模拟数据库
    private static Map<Integer,Employee> employees =null;
    @Autowired
    private DepartmentDao departmentDao;
    static{
        employees=new HashMap<Integer,Employee>();
        employees.put(1001,new Employee(1001,"Aa","A375254509@qq.com",1,new Department(101,"教学部"),new Date()));
        employees.put(1002,new Employee(1002,"bb","B375254509@qq.com",1,new Department(102,"市场部"),new Date()));
        employees.put(1003,new Employee(1003,"cc","C375254509@qq.com",1,new Department(103,"校验部"),new Date()));
        employees.put(1004,new Employee(1004,"dd","D375254509@qq.com",1,new Department(104,"开发部"),new Date()));
        employees.put(1005,new Employee(1005,"ee","E375254509@qq.com",1,new Department(105,"实验部"),new Date()));
    }
    //主键自增
    private static Integer initId=1006;
    //增加员工
    public void save(Employee employee){
        if (employee.getId()==null){
            employee.setId(initId++);
        }
        employee.setDepartment(departmentDao.getDepartmentById(employee.getDepartment().getId()));
        employees.put(employee.getId(),employee);
    }
    //查询全部员工
    public Collection<Employee> getAll(){
        return employees.values();
    }
    //通过id查询
    public Employee getEmployeeById(int id){
        return employees.get(id);
    }
    //删除
    public void delete(int id){
        employees.remove(id);
    }
    //修改
    public void update(Employee employee){
        employees.put(employee.getId(),employee);
    }


}
